package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.ReportDao;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.ReportService;
import com.itheima.health.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author likepei
 * @Date 2020/2/21 10:52
 * @Version v1.0
 * @Description 图表的业务实现类
 */
@Service
@Slf4j
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportDao reportDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    /**
     * 获取指定月份列表的 会员数量
     *
     * @param monthList
     * @return
     */
    @Override
    public List<Integer> findMemberCountByMonth(List<String> monthList) {
        List<Integer> memberCount = new ArrayList<>();

        //遍历获取到 每一个月份
        for (String month : monthList) {
            // month ===>  2020.01
            month = month + ".31"; // 拼接后的日期 格式 2020.01.31

            //调用dao, 得到指定月份的会员累计数量
            Integer count = reportDao.findMemberCounutBeforeDate(month);
            memberCount.add(count);
        }

        return memberCount;
    }

    /**
     * 套餐占比数据
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return reportDao.findSetmealCount();
    }

    /**
     * 获取运营统计数据
     * Map数据格式：
     * reportDate -> String 				// 报表日期
     * todayNewMember -> Integer			// 今日新增会员
     * thisWeekNewMember -> Integer		// 本周新增会员
     * thisMonthNewMember -> Integer		// 本月新增会员
     * totalMember -> Integer		    	// 总会员
     * todayOrderNumber -> Integer			// 今日预约人数
     * thisWeekOrderNumber -> Integer		// 本周预约人数
     * thisMonthOrderNumber -> Integer		// 本月预约人数
     * todayVisitsNumber -> Integer		// 今日到诊人数
     * thisWeekVisitsNumber -> Integer		// 本周到诊人数
     * thisMonthVisitsNumber -> Integer	// 本月到诊人数
     * hotSetmeal -> List<Map<String,Object>>	// 热门套餐
     *
     * @return
     */
    @Override
    public Map<String, Object> getBusinessReportData() {
        try {
            //获得今天日期
            //String today = "2020-02-22";
            String today = DateUtils.parseDate2String(DateUtils.getToday());
            //获得本周第一天日期(周一)
            //String thisWeekMonday = "2020-02-17";
            String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());

            //获得本月第一天日期
            //String thisMonthFirst = "2020-02-01";
            String thisMonthFirst = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>today = " + today);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>thisWeekMonday = " + thisWeekMonday);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>thisMonthFirst = " + thisMonthFirst);

            //获取与会员有关数据 (今日、本周、本月、总人数)
            Integer todayNewMember = memberDao.findMemberCountByDate(today);
            Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
            Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(thisMonthFirst);
            Integer totalMember = memberDao.findMemberTotalCount();

            //获取与预约有关数据 (今日、本周、本月)
            Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
            Integer thisWeekOrderNumber = orderDao.findOrderCountByAfterDate(thisWeekMonday);
            Integer thisMonthOrderNumber = orderDao.findOrderCountByAfterDate(thisMonthFirst);

            //获得到诊相关数据 (今日、本周、本月)
            Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
            Integer thisWeekVisitsNumber = orderDao.findVisitsCountByAfterDate(thisWeekMonday);
            Integer thisMonthVisitsNumber = orderDao.findVisitsCountByAfterDate(thisMonthFirst);

            //获得套餐热门数据
            List<Map> hotSetmeal = orderDao.findHotSetmeal();

            //封装数据到map集合
            Map<String, Object> resultMap = new HashMap<>();
            //封装报表日期
            resultMap.put("reportDate", today);
            //会员有关数据 (今日、本周、本月、总人数)
            resultMap.put("todayNewMember", todayNewMember);
            resultMap.put("thisWeekNewMember", thisWeekNewMember);
            resultMap.put("thisMonthNewMember", thisMonthNewMember);
            resultMap.put("totalMember", totalMember);
            //获取与预约有关数据 (今日、本周、本月)
            resultMap.put("todayOrderNumber", todayOrderNumber);
            resultMap.put("thisWeekOrderNumber", thisWeekOrderNumber);
            resultMap.put("thisMonthOrderNumber", thisMonthOrderNumber);
            //到诊相关数据 (今日、本周、本月)
            resultMap.put("todayVisitsNumber", todayVisitsNumber);
            resultMap.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
            resultMap.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
            //套餐热门数据
            resultMap.put("hotSetmeal", hotSetmeal);

            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 获取会员性别统计
     *
     * @return Map k：性别  v:数量
     */
    @Override
    public List<Map<String, String>> getMemberSexReport() {
        // { name:null,value:2 }
        List<Map<String, String>> memberSexReport = memberDao.getMemberSexReport();

        for (Map<String, String> stringIntegerMap : memberSexReport) {
            //stringIntegerMap 里边是map k:sex / count
            String sexTab = stringIntegerMap.get("name");
            String sexName;
            if (sexTab != null) {
                switch (sexTab) {
                    case "1":
                        sexName = "男";
                        break;
                    case "2":
                        sexName = "女";
                        break;
                    default:
                        sexName = "其他";
                        break;
                }
            } else {
                sexName = "未知";
            }
            stringIntegerMap.put("name", sexName);
        }


        return memberSexReport;
    }

    /**
     * 获取会员年龄统计
     * 数据格式 ：[{name:value},{name:value}]
     *
     * @return [{name:value},{name:value}]
     */
    @Override
    public Map<String, Object> getMemberAgeReport() {
        Map<String, Object> map = memberDao.getMemberAgeReport(new Date());

        return map;
    }

    @Override
    public Map<String, Object> setBetweenDate(String[] date) {
        log.info("++++++++++++++++++++"+date[0]);
        //拿到从前端选的第一天的日期年份月份和天
        String[] day1 = date[0].split("-");

        try {
            //建立Map和List封装返回值
            Map<String,Object> map = new HashMap<>();
            List<String> xAxis = new ArrayList<>();
            List<String>series = new ArrayList<>();
            //得到相差的天数
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date d1 = sdf.parse(date[0]);
            Date d2 = sdf.parse(date[1]);
            Long differ = ( d2.getTime() - d1.getTime() ) / (1000 * 60 * 60 * 24);
            log.info("++++++++++++++++"+differ);
            //这之前都在整选中两天的时间差

            log.info("+++++++"+day1[0]+"++++++++");
           Calendar calendar1 = Calendar.getInstance();
           //设置年月日的容器
            int year = Integer.parseInt(day1[0]);
            int month = Integer.parseInt(day1[1]);
            int day = Integer.parseInt(day1[2]);
            //设置前端传过来的日期
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR,year);
            calendar.set(Calendar.MONTH,month-1);
            calendar.set(Calendar.DAY_OF_MONTH,day);
            if (differ<=14){
                //比利调Dao
                for (int i = 0; i <= differ; i++) {
                    //将calendar转换为String类型
                    String dateStr = sdf.format(calendar.getTime());
                    xAxis.add(dateStr);
                    String count = reportDao.findMemberByDate(dateStr);
                    series.add(count);
                    calendar.add(Calendar.DAY_OF_MONTH,1);
                }
            }else if (differ<=28) {
                for (int i = 0; i <= differ; i += 2) {
                    //将calendar转换为String类型
                    String dateStr = sdf.format(calendar.getTime());
                    xAxis.add(dateStr);
                    String count = reportDao.findMemberByDate(dateStr);
                    series.add(count);
                    calendar.add(Calendar.DAY_OF_MONTH, 2);
                }
            }else if (differ<=42) {
                for (int i = 0; i <= differ; i += 3) {
                    //将calendar转换为String类型
                    String dateStr = sdf.format(calendar.getTime());
                    xAxis.add(dateStr);
                    String count = reportDao.findMemberByDate(dateStr);
                    series.add(count);
                    calendar.add(Calendar.DAY_OF_MONTH, 3);
                }
            }else{
                for (int i = 0; i <= differ; i ++) {
                    //将calendar转换为String类型
                    //如果查询的日期大于42天，就将按月计算，从开始时间到现实的当前时间
                    //但不能查超过14个月前的，别搞我
                    calendar.set(Calendar.DAY_OF_MONTH,1);
                    String dateStr = sdf.format(calendar.getTime());
                    xAxis.add(dateStr);
                    String count = reportDao.findMemberByDate(dateStr);
                    series.add(count);
                    calendar.add(Calendar.MONTH, 1);
                    //获取calendar的Date数值
                    Date datebeginadd1 = calendar.getTime();
                    Date dateNow = new Date();
                    //判断加一个月的时间是否大于当前时间
                    if (datebeginadd1.getTime()>dateNow.getTime()){
                        break;
                    }
                }
            }
            map.put("xAxis",xAxis);
            map.put("series",series);
            //log.info("+++++++++++++++{}++++++++++++",member);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
